<template>
    <view class="container">
        <view class="avatar">
            <image src="/static/images/avatar.png"></image>
        </view>

        <view class="form">
            <u--form labelPosition="left" :model="business" :rules="rules" ref="login">
                <u-form-item label="手机" prop="mobile" borderBottom>
                    <u--input v-model="business.mobile" border="none" placeholder="请输入手机号"></u--input>
                </u-form-item>

                <u-form-item label="密码" prop="password" borderBottom>
                    <u--input type="password" v-model="business.password" border="none" placeholder="请输入密码"></u--input>
                </u-form-item>

                <view style="margin-top: 25px;">
                    <u-button type="warning" @click="submit">登录</u-button>
                </view>
            </u--form>
            
        </view>

        <!-- 信息提示 -->
		<u-toast ref="notice"></u-toast>
    </view>
</template>
<script>
export default {
    onReady() 
    {
        //onReady 为uni-app支持的生命周期之一
        this.$refs.login.setRules(this.rules)
    },
    data() {
        return {
            business:{
                mobile:'18888888889',
                password:'123456'
            },
            rules:{
                mobile: [
                {
                    // 自定义验证函数，见上说明
                    validator: (rule, value, callback) => {
                        // 上面有说，返回true表示校验通过，返回false表示不通过
                        // uni.$u.test.mobile()就是返回true或者false的
                        if (value) {
                            return uni.$u.test.mobile(value);
                        } else {
                            return false;
                        }
                    },
                    type: 'string',
                    required: true,
                    message: '手机号码不正确',
                    // 触发器可以同时用blur和change
                    trigger: ['change', 'blur'],
                }],
                password: {
                    type: 'string',
                    required: true,
                    message: '请填写密码',
                    trigger: ['blur', 'change']
                },
            },
        }
    },
    methods: {
        async submit()
        {
            //调用表单效验的方法
            this.$refs.login.validate().then(async () => {
                var result = await uni.$u.http.post('business/WebLogin', this.business)

                if(result.code == 0)
                {
                    this.$refs.notice.show({
                        type: 'error',
                        message: result.msg
                    })
                    return false;
                }

                //保存本地存储，回到上一个界面
                uni.setStorageSync('token', result.data.token)

                this.$refs.notice.show({
                    type: 'success',
                    message: result.msg,
                    complete: () => uni.$u.route({type: 'back', delta: 1})
                })
            }).catch(err => {
                console.log(err)
                this.$refs.notice.show({
                    type: 'error',
                    message: '表单未通过效验或者是then回调逻辑有问题'
                })
            })
            
        },
    },
}
</script>

<style>
page{
    background-color: #f8fafe;
}

.container .avatar {
    text-align: center;
    background-color: #fff;
}

.avatar image{
    width: 120px;
    height: 120px;
    margin: 50px 0;
    border-radius: 50%;
}

.form{
    margin: 0 50rpx;
    box-sizing: border-box;
}

.btn{
    margin-top: 25px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.u-icon{
    margin-left: 10px;
}
</style>